package com.system.presideover.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.system.common.result.Result;
import com.system.presideover.entity.Emcee;
import com.system.presideover.entity.Orders;
import com.system.presideover.service.EmceeService;
import com.system.presideover.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户预约 前端控制器
 * </p>
 *
 * @author system
 * @since 2024-04-16
 */
@Api(tags = "用户预约")
@RestController
@RequestMapping("/presideover/orders")
public class OrdersController {


    @Autowired
    private OrdersService ordersService;

    @Autowired
    private EmceeService emceeService;

    @ApiOperation(value = "用户预约")
    @GetMapping("/listOrders")
    public Result listOrders(){
        List<Orders> list = ordersService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "添加用户预约")
    @PostMapping("/saveOrders/{emceeid}")
    public Result saveOrders(@RequestBody Orders accountNumber,@PathVariable Integer emceeid){

        Emcee byId = emceeService.getById(emceeid);
        accountNumber.setEmceeId(emceeid);
        accountNumber.setEmceeName(byId.getName());
        boolean save = ordersService.save(accountNumber);

        if (save){
            return Result.ok().message("添加成功");
        }
        return Result.fail().message("添加失败");
    }

    @ApiOperation(value = "更改用户状态成功")
    @PostMapping("/updateWin")
    public Result updateWin(@RequestBody Orders accountNumber){

        accountNumber.setStart(1);
        boolean save = ordersService.updateById(accountNumber);

        if (save){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }


    @ApiOperation(value = "更改用户状态失败")
    @PostMapping("/updatelose")
    public Result updatelose(@RequestBody Orders accountNumber){

        accountNumber.setStart(2);
        boolean save = ordersService.updateById(accountNumber);

        if (save){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }


//    @ApiOperation(value = "修改用户预约")
//    @PostMapping("/updateOrders")
//    public Result updateOrders(@RequestBody Orders accountNumber){
//        boolean save = ordersService.updateById(accountNumber);
//
//        if (save){
//            return Result.ok().message("修改成功");
//        }
//        return Result.fail().message("修改失败");
//    }

    @ApiOperation(value = "删除用户预约")
    @PostMapping("/delectOrders")
    public Result delectOrders(@RequestBody Orders accountNumber){
        boolean save = ordersService.removeById(accountNumber.getId());

        if (save){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }


    @ApiOperation(value = "删除用户预约")
    @PostMapping("/filterOrders/{emceeid}/{emceeName}")
    public Result filterOrders(@PathVariable Integer emceeid,@PathVariable String emceeName){

        List<Orders> orders = null;
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        if (emceeid!=null && !StringUtils.isEmpty(emceeName)){
            wrapper.eq(Orders::getEmceeId,emceeid).like(Orders::getEmceeName,emceeName);
            orders = ordersService.list(wrapper);
        }

//        if (emceeid!=null){
//            wrapper.eq(Orders::getEmceeId,emceeid);
//            orders = ordersService.list(wrapper);
//        }
//        if (!StringUtils.isEmpty(emceeName)){
//            wrapper.like(Orders::getEmceeName,emceeName);
//            orders = ordersService.list(wrapper);
//        }

            return Result.ok(orders);
    }

}

